import Router from "koa-router";
import { authMiddleware } from "../../middleware/auth";
import { checkAdminPermission, successResponse, handleError } from "./common";

// 导入各个admin子模块
import userRoutes from "./user";
import productRoutes from "./product";
import orderRoutes from "./order";
import cartRoutes from "./cart";
import ipRoutes from "./ip";
import dashboardRoutes from "./dashboard";
import chatRoutes from "./chat";
import aiChatRoutes from "./aiChat";

const router = new Router({ prefix: "/api/admin" });

// Admin首页信息
router.get("/", authMiddleware, async (ctx) => {
  if (!checkAdminPermission(ctx)) return;
  
  try {
    successResponse(ctx, {
      message: "欢迎使用后台管理系统",
      version: "1.0.0",
      modules: [
        { name: "用户管理", path: "/api/admin/users", description: "管理用户账户、权限和统计" },
        { name: "商品管理", path: "/api/admin/products", description: "管理商品、配置和库存" },
        { name: "订单管理", path: "/api/admin/orders", description: "处理订单状态和支付管理" },
        { name: "购物车管理", path: "/api/admin/carts", description: "查看和清理用户购物车" },
        { name: "IP日志管理", path: "/api/admin/ip-logs", description: "监控访问日志和安全分析" },
        { name: "聊天管理", path: "/api/admin/chat", description: "管理客服聊天记录和消息状态" },
        { name: "AI对话管理", path: "/api/admin/ai-chat", description: "管理AI对话记录和会话数据" },
        { name: "系统仪表盘", path: "/api/admin/dashboard", description: "服务器监控和系统状态" }
      ],
      adminInfo: {
        adminId: ctx.state.user.id,
        adminName: ctx.state.user.username,
        loginTime: new Date().toISOString()
      }
    }, "后台管理系统");
    
  } catch (error) {
    handleError(ctx, error, "获取管理后台信息失败");
  }
});

// 注册各个子模块路由
router.use(userRoutes.routes());
router.use(productRoutes.routes());
router.use(orderRoutes.routes());
router.use(cartRoutes.routes());
router.use(ipRoutes.routes());
router.use(chatRoutes.routes());
router.use(aiChatRoutes.routes());
router.use(dashboardRoutes.routes());

export default router; 